package com.pureut.storage.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.storage.domain.PickingReturnsDetail;
import com.pureut.storage.dto.PickingReturnsDetailListDto;
import com.pureut.storage.mapper.PickingReturnsDetailMapper;
import com.pureut.storage.service.PickingReturnsDetailService;
import com.sale.common.core.constant.CacheNames;
import com.sale.common.dict.utils.DictUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.CacheUtils;
import com.sale.system.api.RemoteUnitService;
import com.sale.system.api.domain.SysAuxiliary;
import com.sale.system.api.domain.SysDictData;
import com.sale.system.api.model.MaterialUnitDto;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: YSL
 * @Date: 2023/2/3 17:53
 * @Desc: 委外退货明细
 */
@Service
public class PickingReturnsDetailServiceImpl extends ServiceImpl<PickingReturnsDetailMapper, PickingReturnsDetail> implements PickingReturnsDetailService {

    /**
     * 委外退货明细mapper
     */
    @Resource
    PickingReturnsDetailMapper pickingReturnsDetailMapper;


    @Resource
    RemoteUnitService remoteUnitService;

    /**
     * 查询委外退货明细列表
     * @param docNum
     * @return
     */
    @Override
    public TableDataInfo<PickingReturnsDetailListDto> getReturnsList(String docNum, PageQuery pageQuery) {

        Page<PickingReturnsDetailListDto> returnsList = pickingReturnsDetailMapper.getReturnsList(docNum, pageQuery.build());

        // 委外退货明细物料状态
        List<SysDictData> statusArray = DictUtils.getDictCache("picking_returns_detail");
        Map<String, String> materialMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //单位集合
        Map<String, MaterialUnitDto> unitByCodeToMap = remoteUnitService.getUnitByCodeToMap();


        List<PickingReturnsDetailListDto> records = returnsList.getRecords();
        for (PickingReturnsDetailListDto pickingReturnsDetailListDto : records){
            // 物料状态
            pickingReturnsDetailListDto.setWodStatusDict(materialMap.get(String.valueOf(pickingReturnsDetailListDto.getWodStatus())));

            //单位
            if(pickingReturnsDetailListDto.getMaterialUnit() != null){
                pickingReturnsDetailListDto.setMaterialUnit(unitByCodeToMap.get(pickingReturnsDetailListDto.getMaterialUnit()).getUnitName());
            }

            //辅助属性
            List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + pickingReturnsDetailListDto.getTenantId(), String.valueOf(pickingReturnsDetailListDto.getMaterialId()));
            pickingReturnsDetailListDto.setAuxiliaryAttribute(statsArray);
        }

        return TableDataInfo.build(returnsList);
    }
}
